<?php
declare (strict_types=1);

namespace app\api\controller;

use app\admin\model\File;
use app\api\model\Member;
use app\common\Alisms;
use think\facade\Db;
use wechat\jssdk;

/**
 * 公共
 */
class Common extends Api
{
    /**
     * {@inheritdoc}
     * rules:
     * actions 控制器列表: * 全部控制器
     * roles 允许访问角色:
     * all 全部;
     * no_all 不允许任何人进;
     * login 登录(默认);
     * allow 验证规则:
     * no_checkTime 不验证时间戳;
     * no_checkSign  不验证签名;
     */
    protected $auth = [
        [
            'actions' => ['*'],
            'roles' => 'all',
            'allow' => [],
        ]
    ];

    /**
     * 上传图片
     * @param file
     * @return void
     */
    public function uploadImage()
    {
        $error = "";
        $result = upload_image('file', 'front', $error);
        if (!$result) {
            $this->error($error);
        }
        $this->success("上传成功", $result);
    }

    /**
     * 文字描述
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function cont()
    {
        if (!$this->request->isPost()) $this->error("请求方式错误");

        //获取文字描述
        $data = Db::name("cont")
            ->field('id,title,content')
            ->where('mark', 1)
            ->select();
        $this->result('获取成功',$data,0);

    }

    // 1 关于我们 2隐私协议 3用户协议
    public function contDetail()
    {
        $param = $this->request->param();
        //获取文字描述
        $data = Db::name("cont")
            ->where('id', $param['type'])
            ->field('id,title,cont')
            ->where('mark', 1)
            ->find();
        $this->result('获取成功',$data,0);

    }


    /**
     * 获取筛选项
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function filterList()
    {
        $params = request()->param();
        $id = getter($params, 'id');
        $where = [];
        if ($id) {
            $where[] = ['id', '=', $id];
        }
        $list = Db::name("dict")
            ->where('mark', 1)
            ->field("id,name,code")
            ->where('mark', 1)
            ->where($where)
            ->select()
            ->toArray();
        foreach ($list as $key => $value) {
            $list[$key][$value['code']] = Db::name("dict_data")->where('dict_id', $value['id'])->where('mark', 1)->field('id,name')->select()->toArray();
        }
        $this->result('获取成功',$list,0);
    }

    /**
     * 轮播图
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function banner()
    {
        $params = request()->param();
        $res = Db::name("banner")
            ->where('mark', 1)
            ->where('status', 1)
            ->where('type', $params['type'])
            ->field('id,icon,url')
            ->select()
            ->toArray();
        if (!empty($res)) {
            foreach ($res as $k => $v) {
                $res[$k]['icon'] = get_image_url($v['icon']);
            }
        }
        $this->result('获取成功',$res,0);
    }

    //帮助中心
    public function helpList()
    {
        $list = Db::name("help")->field("id,title,context")->order("top desc,sort asc")->where('mark',1)->select()->toArray();
        $this->result('获取成功',$list,0);
    }

    /**
     * 版本更新
     * @return void
     */
    public function versionUpdate()
    {
        $params = request()->param();
        if ($params['type'] == 1) {
            $version = \app\api\model\Common::config("Android");
            $url = \app\api\model\Common::config("Android_url");
        } else {
            $version = \app\api\model\Common::config("IOS");
            $url = \app\api\model\Common::config("IOS_url");
        }
        $data['url'] = "";
        $data['status'] = false;
        if ($version !== $params['version']) {
            $data['url'] = $url;
            $data['status'] = true;
        }
        $this->result('获取成功',$data,0);
    }


}


